package com.valten.controller;

import com.valten.anno.ControllerLog;
import com.valten.dao.UserDao;
import com.valten.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Collection;

@RestController
public class WebController {

    @RequestMapping("/hello")
    public String hello(@PathParam("name") String name, @PathParam("age") int age) {
        return "Hello " + name + ", 今年" + age + "岁";
    }

    @RequestMapping("/test")
    @ControllerLog(description = "测试异常", operateType = "4")
    public String test(@RequestParam("a") Integer a, @RequestParam("b") Integer b) {
        int i = a / b;
        return "Hello";
    }

    @Autowired
    private UserDao userDao;

    @GetMapping("/user/list")
    @ControllerLog(description = "用户列表", operateType = "4")
    public Collection<User> getUsers(){
        return userDao.getUsers();
    }

    @GetMapping("/user/{id}")
    @ControllerLog(description = "用户详情", operateType = "4")
    public User getUserById(@PathVariable("id") Integer id){
        return userDao.getuserById(id);
    }

    @PostMapping("/user/add")
    @ControllerLog(description = "用户新增", operateType = "1")
    public void addUser(@RequestBody User user){
        userDao.addUser(user);
    }

    @DeleteMapping("/user/{id}")
    @ControllerLog(description = "用户删除", operateType = "2")
    public void delUser(@PathVariable("id") Integer id){
        userDao.delUser(id);
    }

    @PatchMapping("/user/update")
    @ControllerLog(description = "用户修改", operateType = "3")
    public void updateUser(User user){
        userDao.addUser(user);
    }
}
